/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.commons.util.exportExcel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import jxl.Cell;
import jxl.Workbook;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TMoneyInOut;
import com.ruipengkj.commons.util.DateFormatUtil;
import com.ruipengkj.service.TBillDetailService;
import com.ruipengkj.service.TEmployeeService;
/**
 * 说明：
 * 导出盘点入库单汇总表
 * @Author：
 * @Date：2012-03-21
 */
public class MoneyChargeOrPayExportUtil {
	private static final long serialVersionUID = 1L;
	
	public static void writeExcel(String fileName,List<TMoneyInOut> list, boolean type,TBillDetailService tBillDetailService,TEmployeeService tEmployeeService){
		if (list !=null&&list.size()!=0) {
			WritableWorkbook wwb = null;
			try {
				// 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
				wwb = Workbook.createWorkbook(new File(fileName));
				if (wwb != null) {
					WritableSheet ws = wwb.createSheet("sheet1", 0);
					addRowTop(ws, type);//添加行头
					for (int i = 0; i < list.size(); i++) {
						TMoneyInOut tMoneyInOut =list.get(i);
						tMoneyInOut.setGross(getMoney(tMoneyInOut.getSourceType(), tMoneyInOut.getSource(),tBillDetailService));
						List<WritableCell> labelList=new ArrayList<WritableCell >();
						labelList.add(new Label(0, i+1, tMoneyInOut.getInOutID()));//编号
						
						TEmployee tEmployee=null;
						if(tMoneyInOut.getPersonID()!=null){
							try {
								tEmployee=tEmployeeService.getNameByIdService(tMoneyInOut.getPersonID());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						if(tEmployee==null){
							labelList.add(new Label(1, i+1, ""));
						}else{
							labelList.add(new Label(1, i+1,tEmployee.getName()));//收款人/付款人
						}
						labelList.add(new Number(2, i+1,tMoneyInOut.getGross()));//总额
						labelList.add(new Label(3, i+1,DateFormatUtil.formatF(tMoneyInOut.getHappenTime())));//发生日期
						for(int j=0;j<labelList.size();j++){
							ws.addCell(labelList.get(j));
						}
						ws.setRowView(i+1, 500);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "另一个程序正在使用此文件，进程无法访问");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					// 从内存中写入文件中S
					wwb.write();
					// 关闭资源，释放内存
					wwb.close();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (WriteException e) {
					e.printStackTrace();
				}
			}
		}  
    }   
	private static int getMoney(byte type, String Source,TBillDetailService tBillDetailService) {//计算总金额
		try {
			byte billType = 0;//记录类型
			switch (type) {
				case 1://采购付款
					billType = ApplicationContext.BILL_BILLTYPE1;
					break;
				case 2://2为采购退货引起收款
					billType = ApplicationContext.BILL_BILLTYPE2;
					break;
				case 3://3为销售引起的收款 
					billType = ApplicationContext.BILL_BILLTYPE3;
					break;
				case 4://4为销售退货引起的出款
					billType = ApplicationContext.BILL_BILLTYPE4;
					break;
				case 5://5为充值的出款
					billType = ApplicationContext.BILL_BILLTYPE9;
					break;
			}
			if (Source != null && !Source.equals("")) {
				TBillDetail tBillDetail = new TBillDetail();
				if (type != 5)
					tBillDetail.setBillID(Source);
				else
					tBillDetail.setDetailID(Source);
				tBillDetail.setBillType(billType);
				List<TBillDetail> billDetailList = tBillDetailService.findMoneyService(tBillDetail);
				if (billDetailList != null &&  billDetailList.size()!= 0) {
					int length = billDetailList.size();
					int money = 0;
					for (int i = 0; i < length; ++i) {
						//计算金额
						money += billDetailList.get(i).getNum() * billDetailList.get(i).getPrice();
					}
					return money;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static void addRowTop(WritableSheet ws, boolean type)throws Exception{
		
		WritableFont Bwf = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD, false); //创建大字体：Arial,大小为18号,粗体，非斜体 
		Bwf.setColour(jxl.format.Colour.BLACK); ////字体颜色为红色 
		//创建单元格格式：
		jxl.write.WritableCellFormat CwcfF = new jxl.write.WritableCellFormat(Bwf); 
		CwcfF.setAlignment(jxl.write.Alignment.CENTRE); //设置水平对齐为居中对齐 
		CwcfF.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直对齐为居中对齐 
		CwcfF.setBorder(jxl.format.Border.TOP, BorderLineStyle.MEDIUM); //设置顶部边框线为实线(默认是黑色－－也可以设置其他颜色) 
		CwcfF.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.MEDIUM);//设置右边框线为实线 
		CwcfF.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.MEDIUM); //设置顶部框线为实线 

		
		List<Label> labelList=new ArrayList<Label>();
		labelList.add(new Label(0, 0,"编号",CwcfF));//
		if (type)
			labelList.add(new Label(1, 0,"收款人",CwcfF));//
		else 
			labelList.add(new Label(1, 0,"付款人",CwcfF));//
		labelList.add(new Label(2, 0,"总额",CwcfF));//
		labelList.add(new Label(3, 0,"时间",CwcfF));//
		for(int j=0;j<labelList.size();j++){
			ws.addCell(labelList.get(j));
		}
		for(int i=0;i<ws.getColumns();i++){
			Cell cell=ws.getCell(i, 0);
			ws.setColumnView(i, cell.getContents().length()*4);
		}
//		ws.setRowView(0, 80*4);
	}
}
